package model

import (
	"bytes"
	"errors"
	"github.com/jmoiron/sqlx"
	"strings"
)

// 传感器参数信息
type Sensorparam struct {
	ID          int    `db:"ID"`          //设备参数标识ID
	SensorID    int    `db:"Sensor_ID"`   //设备ID值
	Type        int16  `db:"Type"`        //类别
	Sparams     string `db:"Sparams"`     //参数值
	Description string `db:"Description"` //备注
	CustomTextN string `db:"CustomTextN"` //备注2
}

// 根据【设备参数标识ID】查询【传感器参数信息】表中是否存在相关记录
func (d *Sensorparam) Exist(ID int) (bool, error) {
	rows, err := amp.Queryx("SELECT COUNT(0) Count FROM sensorparam WHERE ID=?", ID)
	if err != nil {
		return false, err
	}
	defer rows.Close()
	count := 0
	if rows.Next() {
		err = rows.Scan(&count)
		if err != nil {
			return false, err
		}
		return count > 0, nil
	}
	return false, nil
}

// 插入单条记录到【传感器参数信息】表中
func (d *Sensorparam) Insert() (int64, error) {
	result, err := amp.Exec("INSERT INTO sensorparam(Sensor_ID,Type,Sparams,Description,CustomTextN) VALUES(?,?,?,?,?)", d.SensorID, d.Type, d.Sparams, d.Description, d.CustomTextN)
	if err != nil {
		return -1, err
	}
	return result.LastInsertId()
}

// 根据【设备参数标识ID】修改【传感器参数信息】表的单条记录
func (d *Sensorparam) Update() (bool, error) {
	result, err := amp.Exec("UPDATE sensorparam SET Sensor_ID=?, Type=?, Sparams=?, Description=?, CustomTextN=? WHERE ID=?", d.SensorID, d.Type, d.Sparams, d.Description, d.CustomTextN, d.ID)
	if err != nil {
		return false, err
	}
	affected, err := result.RowsAffected()
	if err != nil {
		return false, err
	}
	return affected > 0, nil
}

// 根据【设备参数标识ID】查询【传感器参数信息】表中的单条记录
func (d *Sensorparam) Get(ID int) (sensorparam Sensorparam, err error) {
	rows, err := amp.Queryx("SELECT ID, Sensor_ID, Type, Sparams, Description, CustomTextN FROM sensorparam WHERE ID=?", ID)
	if err != nil {
		return sensorparam, err
	}
	defer rows.Close()
	sensorparams, err := d._RowsToArray(rows)
	if err != nil {
		return sensorparam, err
	}
	if len(sensorparams) <= 0 {
		return sensorparam, err
	}
	return sensorparams[0], nil
}

// 根据【设备参数标识ID】数组查询【传感器参数信息】表中的多条记录
func (d *Sensorparam) GetIn(IDs []int) (sensorparams []Sensorparam, err error) {
	if len(IDs) <= 0 {
		return sensorparams, errors.New("IDs is empty")
	}
	sqlStr := bytes.Buffer{}
	sqlStr.WriteString("SELECT ID, Sensor_ID, Type, Sparams, Description, CustomTextN FROM ")
	sqlStr.WriteString("sensorparam")
	sqlStr.WriteString(" WHERE ID IN(")
	paramKeys := strings.Repeat("?,", len(IDs))
	sqlStr.WriteString(paramKeys[:len(paramKeys)-1])
	sqlStr.WriteString(")")
	var rows *sqlx.Rows
	vals := make([]interface{}, 0, len(IDs))
	for _, v := range IDs {
		vals = append(vals, v)
	}
	rows, err = amp.Queryx(sqlStr.String(), vals...)
	if err != nil {
		return sensorparams, err
	}
	defer rows.Close()
	return d._RowsToArray(rows)
}

// 查询【传感器参数信息】表总记录数
func (d *Sensorparam) GetRowCount() (count int, err error) {
	rows, err := amp.Queryx("SELECT COUNT(0) Count FROM sensorparam")
	if err != nil {
		return -1, err
	}
	defer rows.Close()
	if rows.Next() {
		err = rows.Scan(&count)
		if err != nil {
			return -1, err
		}
		return count, nil
	}
	return -1, nil
}

// 分页查询【传感器参数信息】表的记录
func (d *Sensorparam) GetRowList(PageIndex, PageSize int) (sensorparams []Sensorparam, err error) {
	rows, err := amp.Queryx("SELECT ID, Sensor_ID, Type, Sparams, Description, CustomTextN FROM sensorparam LIMIT ?,?", (PageIndex-1)*PageSize, PageSize)
	if err != nil {
		return sensorparams, err
	}
	defer rows.Close()
	return d._RowsToArray(rows)
}

// 解析【传感器参数信息】表记录
func (d *Sensorparam) _RowsToArray(rows *sqlx.Rows) (models []Sensorparam, err error) {
	for rows.Next() {
		mo := Sensorparam{}
		err = rows.Scan(&mo.ID, &mo.SensorID, &mo.Type, &mo.Sparams, &mo.Description, &mo.CustomTextN)
		if err != nil {
			return models, err
		}
		models = append(models, mo)
	}
	return models, err
}
